package com.dycx.cms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.cms.mapper.ArticleKeywordMapper;
import com.dycx.cms.dto.ArticleKeywordDTO;
import com.dycx.cms.entity.ArticleKeywordDO;
import com.dycx.cms.entity.KeywordDO;
import com.dycx.cms.service.ArticleKeywordService;
import com.dycx.cms.service.KeywordService;
import com.dycx.cms.vo.ArticleKeywordVO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文章关键字(ArticleKeyword)表服务实现类
 *
 * @author wuzongbin
 * @since 2023-05-24 15:45:22
 */

@Service
@AllArgsConstructor
public class ArticleKeywordServiceImpl extends ServiceImpl<ArticleKeywordMapper, ArticleKeywordDO> implements ArticleKeywordService {

    private ArticleKeywordMapper articleKeywordMapper;
    private KeywordService keywordService;


    /**
     * 新增数据
     *
     * @param articleId
     * @param keywords
     * @return
     */
    @Override
    public Boolean add(Integer articleId, String keywords) {
        if (articleId == null) return false;
        if (StringUtils.isBlank(keywords)) return false;

        keywords = keywords.replace("，", ",");
        List<String> keyList = StrUtil.splitTrim(keywords, ',', -1);

        if (keyList.size() <= 0) return false;

        LambdaQueryWrapper<ArticleKeywordDO> delWrapper = new LambdaQueryWrapper<>();
        delWrapper.eq(ArticleKeywordDO::getArticleId, articleId);
        this.remove(delWrapper);

        for (int i = 0; i < keyList.size(); i++) {
            String keyword = keyList.get(i);
            LambdaQueryWrapper<KeywordDO> keywordWrapper = new LambdaQueryWrapper<>();
            keywordWrapper.eq(KeywordDO::getKeyword, keyword);
            KeywordDO keywordEntity = keywordService.getOne(keywordWrapper);

            //如果不存在，则添加关键字
            if (keywordEntity == null) {
                keywordEntity = new KeywordDO();
                keywordEntity.setKeyword(keyword);
                keywordEntity.setStatus(true);
                keywordService.save(keywordEntity);
            }

            ArticleKeywordDO addEntity = new ArticleKeywordDO();
            addEntity.setArticleId(articleId);
            addEntity.setSort(i);
            addEntity.setKeywordId(keywordEntity.getId());

            this.save(addEntity);
        }

        return true;
    }

    /**
     * 修改数据
     *
     * @param dto 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean edit(ArticleKeywordDTO dto) {
        return this.updateById(BeanUtil.toBean(dto, ArticleKeywordDO.class));
    }


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ArticleKeywordVO getInfo(Integer id) {
        ArticleKeywordDO entity = this.getById(id);
        return BeanUtil.toBean(entity, ArticleKeywordVO.class);
    }

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<ArticleKeywordVO> getPage(ArticleKeywordDTO dto) {
        Page<ArticleKeywordVO> page = new Page<>(dto.getPageIndex(), dto.getPageSize());

        return this.articleKeywordMapper.getPage(page, dto);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Boolean delete(Integer id) {
        return this.removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return this.articleKeywordMapper.getMaxSort();
    }
}

